package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    //新增地址
    public void add(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook1=new AddressBook();
        BeanUtils.copyProperties(addressBook,addressBook1);
        addressBook1.setUserId(userId);
        addressBookMapper.add(addressBook1);
    }

    @Override
    //查询当前登录用户的所有地址信息
    public List<AddressBook> list() {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.list(userId);
    }

    @Override
    //设置默认地址
    public void setDefault(AddressBook addressBook) {
        AddressBook addressBook1 = new AddressBook();
        BeanUtils.copyProperties(addressBook,addressBook1);
        addressBook1.setIsDefault(StatusConstant.ENABLE);
        addressBookMapper.clearAllDefault(0L);
        addressBookMapper.setDefault(addressBook1);
    }

    @Override
    //查询默认地址
    public AddressBook getDefault() {
        return addressBookMapper.getDefault();
    }

    @Override
    //根据id查询地址
    public AddressBook getAddressById(Long id) {
        return addressBookMapper.getAddressById(id);
    }

    @Override
    //根据id修改地址
    public void UpdateAddressById(AddressBook addressBook) {
        addressBookMapper.updateAddressById(addressBook);
    }

    @Override
    //根据id删除地址
    public void deleteAddressById(Long id) {
        addressBookMapper.deleteAddressById(id);
    }

}
